package com.x.erp.dao;

import com.x.erp.model.MultilevelActivityDetail;
import com.x.framework.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 多级营销活动账户明细信息DAO
 */
@Repository
public class MultilevelActivityDetailDao extends BaseDao {

	private final static String selectMultilevelActivityDetailListSql = "SELECT " +
			"emad.* " +
			"FROM " +
			"erp_multilevel_activity_detail emad " +
			"WHERE " +
			"1 = 1 ";
	/**
	 * 查询多级营销活动账户明细列表
	 * @param multilevelActivityDetail
	 * @return
	 * @throws Exception
	 */
	public List<MultilevelActivityDetail> selectMultilevelActivityDetailList(MultilevelActivityDetail multilevelActivityDetail) throws Exception{
		StringBuilder sql = new StringBuilder(selectMultilevelActivityDetailListSql);
		List<Object> params = new ArrayList<>();
		searchCondition(sql, params, multilevelActivityDetail);
		return this.queryForList(sql.toString(), MultilevelActivityDetail.class, params);
	}

	private final static String selectMultilevelActivityDetailNumSql = "SELECT COUNT(emad.USER_ID) FROM erp_multilevel_activity_detail emad WHERE 1=1 ";
	/**
	 * 查询多级营销活动账户明细数量
	 * @param multilevelActivityDetail
	 * @return
	 * @throws Exception
	 */
	public int selectMultilevelActivityNum(MultilevelActivityDetail multilevelActivityDetail) throws Exception{
		StringBuilder sql = new StringBuilder(selectMultilevelActivityDetailNumSql);
		List<Object> params = new ArrayList<>();
		searchCondition(sql, params, multilevelActivityDetail);
		return this.queryForInt(sql.toString(), params);
	}

	private final static String selectMultilevelActivityDetailTopListSql = "SELECT " +
			"cu.USER_NICKNAME, " +
			"cu.USER_MOBILE, " +
			"SUM(emad.DETAIL_MONEY) AS TOTAL_DETAIL_MONEY " +
			"FROM " +
			"erp_multilevel_activity_detail emad, " +
			"x_client.client_user cu " +
			"WHERE " +
			"emad.USER_ID = cu.USER_ID ";
	/**
	 * 查询多级营销活动账户排行榜
	 * @param multilevelActivityDetail
	 * @return
	 * @throws Exception
	 */
	public List<MultilevelActivityDetail> selectMultilevelActivityDetailTopList(MultilevelActivityDetail multilevelActivityDetail) throws Exception{
		StringBuilder sql = new StringBuilder(selectMultilevelActivityDetailTopListSql);
		List<Object> params = new ArrayList<>();
		sql.append("GROUP BY emad.USER_ID ");
		sql.append("ORDER BY SUM(emad.DETAIL_MONEY) DESC ");
		searchCondition(sql, params, multilevelActivityDetail);
		return this.queryForList(sql.toString(), MultilevelActivityDetail.class, params, multilevelActivityDetail);
	}

	/**
	 * 查询条件
	 * @param sql
	 * @param params
	 * @param multilevelActivityDetail
	 */
	private void searchCondition(StringBuilder sql, List<Object> params, MultilevelActivityDetail multilevelActivityDetail) {
		if(multilevelActivityDetail != null) {
			if(multilevelActivityDetail.getUserId() != null){
				sql.append("AND emad.USER_ID = ? ");
				params.add(multilevelActivityDetail.getUserId());
			}
			if(multilevelActivityDetail.getPageSize() != null){
				sql.append("LIMIT ? ");
				params.add(multilevelActivityDetail.getPageSize());
			}
		}
	}

}
